package com.example.mykotlin.base.config.converter

import com.example.mykotlin.base.util.Constant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeParseException
import org.apache.commons.lang3.StringUtils
import org.springframework.core.convert.converter.Converter

class StringToLocalDateTimeConverter : Converter<String, LocalDateTime> {
    override fun convert(source: String): LocalDateTime? {
        return if (StringUtils.isBlank(source)) null else
            try {
                LocalDateTime.parse(source, Constant.DTF)
            } catch (pe: DateTimeParseException) {
                try {
                    LocalDate.parse(source, Constant.DF).atStartOfDay()
                } catch (pe: DateTimeParseException) {
                    null
                }
                // try {
                //     LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))
                // } catch (pe: DateTimeParseException) {
                //     try {
                //         LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay()
                //     } catch (pe: DateTimeParseException) {
                //         try {
                //             LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy/MM/dd")).atStartOfDay()
                //         } catch (pe: DateTimeParseException) {
                //             null
                //         }
                //     }
                // }
            }
    }


}